class UserGroupsController < ApplicationController
  
  # GET /user_groups
  # GET /user_groups.xml
  def index
    @user_groups = UserGroup.find(:all)
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @user_groups }
    end
  end
  
  # GET /user_groups/1
  # GET /user_groups/1.xml
  def show
    @user_group = UserGroup.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user_group }
    end
  end
  
  # GET /user_groups/new
  # GET /user_groups/new.xml
  def new
    @user_group = UserGroup.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user_group }
    end
  end
  
  # GET /user_groups/1/edit
  def edit
    @user_group = UserGroup.find(params[:id])
  end
  
  # POST /user_groups
  # POST /user_groups.xml
  def create
    @user_group = UserGroup.new(params[:user_group])
    
    respond_to do |format|
      if @user_group.save
        @user_group.users << current_user if(current_user!=:false)
        flash[:notice] = 'UserGroup was successfully created.'
        format.html { redirect_to(@user_group) }
        format.xml  { render :xml => @user_group, :status => :created, :location => @user_group }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user_group.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /user_groups/1
  # PUT /user_groups/1.xml
  def update
    @user_group = UserGroup.find(params[:id])
    
    respond_to do |format|
      if @user_group.update_attributes(params[:user_group])
        flash[:notice] = 'UserGroup was successfully updated.'
        format.html { redirect_to(@user_group) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user_group.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /user_groups/1
  # DELETE /user_groups/1.xml
  def destroy
    @user_group = UserGroup.find(params[:id])
    @user_group.destroy
    
    respond_to do |format|
      format.html { redirect_to(user_groups_url) }
      format.xml  { head :ok }
    end
  end
  
  def join_to_group
      @user_group = UserGroup.find(params[:id])
      @user_group.add_user(current_user)
      redirect_to @user_group
  end
  
  def leave_group
      @user_group = UserGroup.find(params[:id])
      @user_group.remove_user(current_user)
      redirect_to @user_group
  end
end
